package com.example.demo.dao.impl;

import com.example.demo.dao.UserDao;
import com.example.demo.model.User;
import jakarta.annotation.Resource;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class UserImpl implements UserDao {
    @Resource
    private JdbcTemplate jdbcTemplate;

    @Override
    public int addUer(User user) {
        String sql = "INSERT into `user`(`user`.user_name,`user`.status) VALUES(?,?)";
        int update = jdbcTemplate.update(sql,user.getUserName(),user.getStatus());
        return update;
    }

    @Override
    public int update(User user) {
        String sql = "UPDATE `user` SET status=? where user_name=?;";
        return jdbcTemplate.update(sql,user.getStatus(),user.getUserName());
    }

    @Override
    public int delete(User user) {
        String sql = "DELETE FROM `user` where user_name=?;";
        return jdbcTemplate.update(sql,user.getUserName());
    }

    @Override
    public int count(User user) {
        String sql = "SELECT COUNT(*) FROM `user` where `status`=?;";
        return jdbcTemplate.queryForObject(sql,Integer.class,user.getStatus());
    }

    @Override
    public List<User> getList(User user) {
        String sql = "SELECT * FROM `user` where `status`=?;";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(User.class),user.getStatus());
    }

    @Override
    public User getUser(User user) {
        String sql = "SELECT * FROM `user` where `user_id`=?;";
        return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(User.class),user.getUserId());
    }

    @Override
    public void batchAddUser(List<Object[]> batchArgs) {
        String sql = "INSERT into `user` (`user`.user_name,`user`.`status`) VALUES(?,?);";
        jdbcTemplate.batchUpdate(sql,batchArgs);
    }
}
